<template>
  <j-modal
    switchFullscreen
    :title='title'
    :width='500'
    :visible='visible'
    :maskClosable='false'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'>
    <template slot="footer">
      <a-button @click="handleCancel">取消</a-button>
      <a-button v-if="!disableSubmit||title=='调整'" :loading="confirmLoading" type="primary" @click="handleOk">确认</a-button>
    </template>
    <a-spin :spinning='confirmLoading'>
      <a-form :form='form'>
        <a-row :gutter='24'>
          <a-col :md='24' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='商品名称'
            >
              <a-input-search  v-decorator="['goodsName']" :disabled='disableSubmit'
                               readOnly enter-button @search="custSearch('superiorUser')" />
            </a-form-item>
          </a-col>
          <a-col :md='24' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='商品编号'
              >
              <a-input :disabled='true' placeholder='请输入' v-decorator="['goodsCode', ]" />
            </a-form-item>
          </a-col>
          <a-col :md='24' :sm='24'>
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="库存单位">
<!--              <a-input placeholder="" :disabled='true' v-decorator="['stockUnit', {}]"/>-->
              <cn-dict-select-tag v-decorator.trim="['basicUnit']" :disabled="true"
                                  :options="{value:'dictDataDesc',label:'dictDataDesc'}"
                                  :triggerChange="true" dictCode="basic_unit" />
            </a-form-item>
          </a-col>
          <a-col :md='24' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='库存数量'
              >
              <a-input-number :min="0" :max="999999"  placeholder='请输入' :disabled='disableSubmit' v-decorator="['inventoryBalance', ]" style="width:100%"/>
            </a-form-item>
          </a-col>
          <a-col :md='24' :sm='24' v-if="title=='调整'">
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='调整值'
            >
              <a-input-number placeholder='请输入' v-decorator="['changeAmount']" style="width:100%"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
    <proChose ref="prochoose" :name='title2' @okbro="okbropro"></proChose>
  </j-modal>
</template>

<script>
import proChose from '@comp/cn-components/ProChose'
import { postAction } from '@/api/manage'
import { pick } from 'lodash'
import JTreeSelect from '@/components/cn-form/JTreeSelect'
import JAreaLinkage from '@comp/cn-form/CnAreaLinkage'
import UploadImage from '@/components/upload-image/UploadImage'
import JImageUpload from '@/components/cn-form/JImageUpload'

export default {
  name: 'proMinPriceModal',
  data() {
    return {
      sendPro: false,
      remarks: '',
      dataSurces: [],
      manobjList: [0],
      areaData: '',
      areaLinkage: '',
      title: '操作',
      title2: '商品',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        input: {
          rules: [
            { required: true, message: ''}
          ]
        },
      },
      addOrUpdateUrl: window._CONFIG['domianSfaURL'] + '/sfaCommodityController/saveOrUpdate',
      change: window._CONFIG['domianSfaURL'] + '/sfaCommodityController/saveOrUpdate'
    }
  },
  components: { JTreeSelect, JAreaLinkage, UploadImage, JImageUpload,proChose },
  created() {
  },
  methods: {
    okbropro(val, status, status2) {
      let objs = JSON.parse(JSON.stringify(val[0]))
      this.form.setFieldsValue({
        goodsCode: objs.goodsCode,
        goodsName: objs.goodsName,
        // stockUnit: objs.basicUnit,
        basicUnit: objs.basicUnit,
      })
    },
    add() {
      this.edit()
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.model.rangePicker = [ this.model.startDate,this.model.endDate]
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, Object.keys(this.model)))//表单初始化，值源于this.model
      })
    },
    handleCancel() {
      this.$emit('close')
      this.visible = false
    },
    custSearch(status) {
      this.$refs.prochoose.rowKey='goodsCode'
      let formconfig = [
        {
          key: 'goodsCode',
          label: '商品编号'
        },
        {
          key: 'goodsName',
          label: '商品名称'
        }
      ]
      let tableconfig = {
        columns: [
          {
            title: '商品编号',
            align: 'center',
            dataIndex: 'goodsCode',
            width: 120,
            ellipsis: true
          },
          {
            title: '商品名称',
            align: 'center',
            dataIndex: 'goodsName',
            width: 120,
            ellipsis: true
          }
        ],
        list: window._CONFIG['domianURL'] + '/goods/cnGoods/findPageList',
        multiple: false,
        columW: 8
      }
      let dataSource = []
      if(this.form.getFieldValue('goodsCode')){
        dataSource = [{
          goodsCode:this.form.getFieldValue('goodsCode'),
          goodsName:this.form.getFieldValue('goodsName')
        }]
      }
      this.$refs.prochoose.edit(formconfig, tableconfig, status,'status2',dataSource)
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, value) => {
        if (!err) {
          this.confirmLoading = true
          let obj = Object.assign({}, this.model, value)
          postAction(this.addOrUpdateUrl, obj).then(res => {
            if (res.success) {
              that.$message.success(res.message, 2)
              that.$emit('ok')
              that.handleCancel()
            } else {
              that.$message.error(res.message, 2)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    }

  }
}
</script>

<style lang='less' scoped>
.info-line {
  display: flex;
  flex-direction: row;
}

.info-line:before {
  content: "";
  flex: 0.1;
  border-bottom: 1px solid #ebedee;
  margin: auto 4px;
}
.info-line:after {
  content: "";
  flex: 0.9;
  border-bottom: 2px solid #ebedee;
  margin: auto 4px;
}
</style>